package com.shop.front.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.shop.common.partner.jiaotai.JiaotaiBaseRespVo;
import com.shop.common.partner.jiaotai.JiaotaiRequest;
import com.shop.front.request.user.*;
import com.shop.front.service.sms.SmsService;
import com.shop.front.service.user.UserService;
import com.shop.front.property.BusinessProps;
import com.shop.front.response.FrontRetEnum;
import com.shop.front.util.IpUtil;
import com.shop.util.RegexUtils;
import com.shop.domain.user.User;
import com.shop.front.consts.FrontConst;
import com.shop.util.CodecUtil;
import com.shop.util.RandomUtil;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(description = "登录注册", position = -1)
@RestController
@RequestMapping(value = "/lr", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class LoginAndRegController {

    @Autowired
    private BusinessProps businessProps;

    @Autowired
    private UserService userService;

    @Autowired
    private SmsService smsService;


    /**
     * 登录密码所需的RSA公钥
     */
    @ApiOperation(value = "登录密码所需的RSA公钥")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @RequestMapping("/publicKey")
    public BaseResponse publicKey() {
        return new BaseResponse<>(businessProps.getLoginRSAPublicKey());
    }


    /**
     * 密码登录
     *
     * @param userLoginReq
     * @return
     */
    @ApiOperation(value = "密码登录")
    @ApiResponses(@ApiResponse(code = 10000, response = User.class, message = "content"))
    @RequestMapping("/login")
    public BaseResponse login(@RequestBody @Validated UserLoginReq userLoginReq, HttpServletRequest request) {
        // IP环境
        // 获取用户Ip地址
        String ip = IpUtil.getIp(request);
        userLoginReq.setUserIp(ip);

        String pwd = "";
        try {
            pwd = CodecUtil.buildRSADecryptByPrivateKey(userLoginReq.getPwd(), businessProps.getLoginRSAPrivateKey());

        } catch (Exception e) {
            log.error("登录密码解密失败！", e);
            return new BaseResponse(FrontRetEnum.USER_PWD_VALID_EXCPTION);
        }

        return userService.loginInfo(userLoginReq, pwd);
    }

    /**
     * 返回图形验证码
     *
     * @param phone
     * @param response
     */
    @ApiOperation(value = "图形验证码")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name = "phone", required = true, value = "手机号", paramType = "body"))
    @GetMapping("/getImgVerify")
    public void getImgVerify(String phone, HttpServletResponse response) {
        if (StringUtils.isNotEmpty(phone)) {
            userService.getRandcode(phone, response);
        }
    }

    /**
     * 验证图形验证码并发送短信验证码
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "验证图形验证码")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", required = true, value = "手机号", paramType = "body"),
            @ApiImplicitParam(name = "verifyCode", required = true, value = "图形验证码", paramType = "body")})
    @PostMapping("/inspectImgVerify")
    public BaseResponse inspectImgVerify(@RequestBody Map<String, String> map) {
        if (StringUtils.isNotEmpty(map.get("phone")) && StringUtils.isNotEmpty(map.get("verifyCode"))) {
            return userService.inspectImgVerify(map.get("phone"), map.get("verifyCode"));
        }
        return new BaseResponse(FrontRetEnum.PARAM_IS_ERROR);
    }


    /**
     * 验证图形验证码并发送短信验证码
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "验证短信验证码")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", required = true, value = "手机号", paramType = "body"),
            @ApiImplicitParam(name = "code", required = true, value = "短信验证码", paramType = "body")})
    @PostMapping("/checkSmsCode")
    public BaseResponse checkSmsCode(@RequestBody Map<String, String> map) {
        if (StringUtils.isNotEmpty(map.get("phone")) && StringUtils.isNotEmpty(map.get("code"))) {
            return userService.checkSmsCode(map.get("phone"), map.get("code"));
        }
        return new BaseResponse(FrontRetEnum.PARAM_IS_ERROR);
    }


    /**
     * 用户注册
     *
     * @return
     */
    @ApiOperation(value = "用户注册或登录")
    @ApiResponses(@ApiResponse(code = 10000, response = User.class, message = "content"))
    @RequestMapping("/registerOrLogin")
    public BaseResponse register(@RequestBody @Validated UserRegisterReq userRegisterReq, HttpServletRequest request) {
        // 获取用户Ip地址
        String ip = IpUtil.getIp(request);
        userRegisterReq.setUserIp(ip);
        return userService.sendPhoneCode(userRegisterReq);
    }

    /**
     * 重置密码
     *
     * @param userRegisterReq
     * @return
     */
    @ApiOperation(value = "重置密码")
    @ApiResponses(@ApiResponse(code = 10000, response = User.class, message = "content"))
    @RequestMapping("/resetPwd")
    public BaseResponse resetPwd(@RequestBody @Validated UserRegisterReq userRegisterReq) {
        return userService.resetPwd(userRegisterReq);
    }

    /**
     * 登出
     *
     * @return
     */
    @ApiOperation(value = "登出")
    @RequestMapping("/loginOut")
    public BaseResponse loginOut() {
        return userService.loginOut();
    }





/*    *//**
     * 微信登录
     *
     * @param loginLog
     * @return
     *//*
    @ApiOperation(value = "微信登录")
    @ApiResponses(@ApiResponse(code = 10000, response = User.class, message = "content"))
    @PostMapping("/wxLogin")
    public BaseResponse userWeiXinLogin(@RequestBody @Validated TLoginLog loginLog, HttpServletRequest request) {
        // 判断传值是否为空
        if (StringUtils.isNotEmpty(loginLog.getCode())) {
            // 获取用户Ip地址
            String ip = IpUtil.getIp(request);
            loginLog.setUserIp(ip);
            return userService.weixinLogin(loginLog);
        }
        return new BaseResponse(FrontRetEnum.PARAM_IS_ERROR);
    }*/


}
